<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class SalesOrderTypeRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'type_desc' => 'required|max:100',
        ];
        switch (request()->method) {
            case 'POST':
                $rules['type'] = 'required|max:4|unique:sales_order_type,type';
                break;
            case 'PUT':
                $rules['type'] = 'required|max:4|unique:sales_order_type,type,' . request()->sales_order_type;
                break;
            default:
                break;
        }

        return $rules;
    }

    public function messages()
    {
        return [
            'type.required' => '请输入销售订单类型代码！',
            'type.max' => '销售订单类型代码长度最大为4个字符！',
            'type.unique' => '销售订单类型代码已经存在！',
            'type_desc.required' => '请输入销售订单类型描述！',
            'type_desc.max' => '销售订单类型描述长度最大为100个字符！',
        ];
    }
}
